import requests, re, os


class App:

    def __init__(self, zxsq=None):

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'}

        url = 'https://www.renrendoc.com/paper/263757877.html'

        html = requests.get(url=url, headers=headers).text

        data = self.data_re(html)

        title = self.title_re(html)

        if not os.path.exists(title):
            os.mkdir(title)

        print(data)

        self.txt(data, title)

        gif_url = re.findall(r'value="(https:.*?)"', html)

        self.pic(gif_url, title, headers)

    def data_re(self, html):

        data = re.findall(r'<div class="content Hidden">(.*?)</div>', html)

        return data

    def title_re(self, html):

        title = re.findall(r'<h1 title="(.*?)"', html)

        return title

    def txt(self, data, title):

        if data:
            path = os.path.join(title, f'{title}.txt')

            f = open(path, 'w', encoding='utf-8')

            f.write(data[0])

            f.close()

    def pic(self, gif_url, title, headers):

        if gif_url:

            for i in range(100):

                url1 = gif_url + str(i + 1) + '.gif'

                response = requests.get(url=url1, headers=headers)

                if response:

                    path_2 = os.path.join(title, f'{i + 1}.jpg')

                    print(path_2)

                    ff = open(path_2, 'wb')

                    ff.write(response.content)

                    ff.close()

                else:

                    break


if __name__ == '__main__':
    app = App()
